<?php

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of MasterController
 *
 * @author Sorasak Siangsanan
 */
class MasterController extends Controller {

    private static function getMasterOrder() {
        $criteria = new CDbCriteria();
//        $criteria->condition = "order_date between '" . date('Y-m-d 00:00:00') . "' AND '" . date('Y-m-d 23:59:59') . "'";
        
        $criteria->order = "price desc, update_date asc";
        $orders = Order::model()->findAllByAttributes(array('status_id' => 4), $criteria);
        return $orders;
    }

    public function actionIndex() {
        $model = new MasterForm();
        $orders = self::getMasterOrder();
        foreach ($orders as $order){
            $cust = Customer::model()->findByPk($order->cust_id);
            $order->orderOwnerName = $cust->Name." ".$cust->Surname ;
        }
        $this->render('index', array('model' => $model, 'orders' => $orders));
    }

    public function actionConfirm() {
        $model = new MasterForm();
        $orderId = $_GET["id"];

        if ($model->confirm($orderId)) {
            $this->redirect(array('master/index'));
        }

        $orders = self::getMasterOrder();
        $this->render('index', array('model' => $model, 'orders' => $orders));
    }

}
